import 'package:flutter/material.dart';
import 'package:manual/pages/layout_page.dart';
import 'package:manual/providers/image_list_provider.dart';
import 'package:provider/provider.dart';
import 'dart:math';

class RefreshListPage extends StatelessWidget {
  const RefreshListPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1,
        title: const Text("List"),
      ),
      body: const ColumnLayout(),
    );
  }
}


class ColumnLayout extends StatefulWidget {
  const ColumnLayout({super.key});

  @override
  State<ColumnLayout> createState() => _ColumnLayoutState();
}

class _ColumnLayoutState extends State<ColumnLayout> with SingleTickerProviderStateMixin {
  final ScrollController _scrollController = ScrollController();
  bool isLoading = false;

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(_onScroll);
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  void _onScroll() {
    final provider = context.read<ImageListProvider>();
    if (_scrollController.position.pixels >= _scrollController.position.maxScrollExtent && !isLoading) {
      setState(() {
        isLoading = true;
      });
      Future.delayed(const Duration(seconds: 2), () {
        // 随机生成 10 个路径
        final random = Random();
        final newPaths = List.generate(
          10,
          (index) => "lib/assets/images/common/img_${(random.nextInt(4) + 1)}.png",
        );
        // 调用 provider.addImage 方法
        for (final path in newPaths) {
          provider.addImage(path);
        }
        setState(() {
          isLoading = false; // 加载完成
        });
      });
    }
  }

  // 下拉刷新方法
  Future<void> _onHadndelRefresh() async {
    final provider = context.read<ImageListProvider>();
    await Future.delayed(const Duration(seconds: 2)); // 延迟两秒
    provider.reset(); // 重置数据
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<ImageListProvider>(
      builder: (context, provider, child) {
        return RefreshIndicator(
          onRefresh: _onHadndelRefresh,
          displacement: 0, // 设置下拉动画的位移为 0
          child: Column(
            children: [
              Text(provider.imgList.length.toString()),
              Expanded(
                child: ListView.builder(
                  controller: _scrollController,
                  itemCount: provider.imgList.length,
                  itemBuilder: (context, index) {
                    final imgData = provider.imgList[index];
                    return ImageLayout(
                      path: imgData["path"] ?? "",
                      id: imgData["id"] ?? "",
                    );
                  },
                ),
              ),
              if (isLoading)
                const Padding(
                  padding: EdgeInsets.all(16.0),
                  child: CircularProgressIndicator(),
                ),
            ],
          ),
        );
      },
    );
  }
}
